﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExtEasy.Base;

namespace ExtEasy.Controls
{
    public class ExtCheckBoxList : ExtControl
    {
        public ExtCheckBoxList(string id)
            : base(id)
        {
            SelectedIDs = new List<string>();
            Items = new List<ExtListItem>();
            SelectAllText = "Выбрать все";
        }

        public List<ExtListItem> Items { get; set; }
        public List<string> SelectedIDs { get;set;}
        public string SelectAllText { get; set; }


        public bool IsItemSelected(ExtListItem li)
        {
            return SelectedIDs.Contains(li.ID);
        }

        public override ExtAjaxRes ProcessOperation(System.Collections.Specialized.NameValueCollection c)
        {
           // foreach(string val in Dictionary.Keys

            bool needrefresh = false;
            string script = string.Empty;
            try
            {               
                switch (c["op"])
                {
                    case "refresh": needrefresh = true; break;

                    

                    default: throw new Exception("Не найдена операция " + c["op"]);
                }
            }
            catch (Exception ex)
            {
                script += "alert('" + ex.Message/* + " " + ex.StackTrace*/+ "');";
            }

            if (needrefresh)
            {
                script += "BindCheckItemEvents();";
            }

            Render();
            return needrefresh ? new ExtAjaxRes { ID = ID, HTML = HTML, BackScript = script } : new ExtAjaxRes { BackScript = script };
        }       
    }    
}
